﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmProductBack : Form
    {

        string realLocationSN;
        public FrmProductBack()
        {
            InitializeComponent();
        }

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        DataTable DtOut;

        DataTable RowDetailDT;

        public void InputBillNo()
        {
            DtOut = Bll_ProductOut.GetOutStockLists(cmb_BillNo.Text);
            DataRow[] drOut = DtOut.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
            if (drOut.Length > 0)
            {
                DataTable info = Bll_ProductOut.GetOutStockInfo(cmb_BillNo.Text);
                if (info.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    {
                        var msg = GetMessage();
                        lab_msg.Text = cmb_BillNo.Text + "详情：" + msg;
                        txt_LocationSN.Focus();
                    }
                    else
                    {
                        MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");
                        cmb_BillNo.Text = "";
                        cmb_BillNo.Focus();
                    }
                }

            }
            else
            {
                lab_msg.Text = cmb_BillNo.Text + "单号不正确,请输入正确的单据!";
                cmb_BillNo.Text = "";
                cmb_BillNo.Focus();
            }
        }

        private void FrmProductBack_Load(object sender, EventArgs e)
        {
            //  BindCMB();
        }

        public string GetMessage()
        {
            var dt = Bll_ProductOut.GetDeliveryInfo(cmb_BillNo.Text);
            var result = string.Empty;

            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    var selectQty = 0;
                    if (!string.IsNullOrEmpty(dr["SelectQty"].ToString()))
                    {
                        selectQty = Convert.ToInt32(dr["SelectQty"]);
                    }
                    var planQty = 0;
                    if (!string.IsNullOrEmpty(dr["PlanQty"].ToString()))
                    {
                        planQty = Convert.ToInt32(dr["PlanQty"]);
                    }

                    sb.AppendLine("物料号:" + dr["ProductNumber"] + ";计划数量:" + planQty + ";实际数量:" + selectQty);
                }
                result = sb.ToString();

            }
            return result;
        }

        private void txt_BoxCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                var splitList = txt_BoxCode.Text.Split('-');
                var boxNo = splitList[0];
                txt_BoxCode.Text = boxNo;

                if (string.IsNullOrEmpty(cmb_BillNo.Text))
                {
                    //lab_msg.Text = "请先输入需要退捡的单号!";
                    MessageBox.Show("请先输入需要退捡的单号!");
                    return;
                }

                RowDetailDT = Bll_ProductOut.GetOutStockRowDetailsInfo(cmb_BillNo.Text, txt_BoxCode.Text);

                if (RowDetailDT.Rows.Count == 0)
                {
                    //lab_msg.Text = txt_BoxCode.Text + "不正确或不属于此单!";
                    MessageBox.Show(txt_BoxCode.Text + "不正确或不属于此单!");
                    txt_BoxCode.Focus();
                    txt_BoxCode.Text = "";
                    return;
                }

                var partNumber = RowDetailDT.Rows[0]["PartNumber"].ToString();
                var status = Bll_ProductIn.GetLocationSNStatus(realLocationSN, partNumber);
                //有库存
                if (status == 1)
                {
                    var returnInventory = Bll_ProductIn.GetInventoryInfo(realLocationSN, partNumber);
                    if (returnInventory != null && returnInventory.Rows.Count > 0)
                    {
                        if (returnInventory.Rows[0]["DateCode"].ToString() != RowDetailDT.Rows[0]["DateCode"].ToString())
                        {
                            MessageBox.Show(txt_LocationSN.Text + "该库位DataCode与退捡产品不符，请重新选择其他库位！");
                            txt_LocationSN.Text = "";
                            txt_LocationSN.Focus();
                            return;
                        }
                    }
                }

                var array = realLocationSN.Split('#');
                var storageSN = array[0];
                var areaSN = array[0] + "#" + array[1];

                if (RowDetailDT.Rows.Count > 0)
                {
                    var backRes = Bll_ProductOut.BackOutStockRowDetails(RowDetailDT, storageSN, areaSN, realLocationSN, status);

                    if (backRes)
                    {
                        lab_msg.Text = "箱号:" + txt_BoxCode.Text + "料号:" + RowDetailDT.Rows[0]["PartNumber"].ToString() + "数量" + RowDetailDT.Rows[0]["Qty"].ToString() + "退捡成功。\r\n退捡之后" + cmb_BillNo.Text + "详情：" + GetMessage();
                        txt_BoxCode.Focus();
                        txt_BoxCode.Text = "";
                    }
                    else
                    {
                        //lab_msg.Text = txt_BoxCode.Text + "退捡失败";
                        MessageBox.Show(txt_BoxCode.Text + "退捡失败,请重试！");
                        txt_BoxCode.Focus();
                        txt_BoxCode.Text = "";
                    }

                }
                else
                {
                    //lab_msg.Text = txt_BoxCode.Text + "不正确或不属于此单!";
                    MessageBox.Show(txt_BoxCode.Text + "不正确或不属于此单!");
                    txt_BoxCode.Focus();
                    txt_BoxCode.Text = "";
                }
            }
        }

        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;

            }
            realLocationSN = txt_LocationSN.Text;

            if (string.IsNullOrEmpty(realLocationSN))
            {
                MessageBox.Show("库位不能为空!");
                return;
            }

            var array = realLocationSN.Split('#');

            if (array.Count() < 3)
            {
                MessageBox.Show("库位格式不正确，请检查确认!");
                return;
            }

            var storageSN = array[0];
            var areaSN = array[0] + "#" + array[1];
            txt_LocationSN.Text = array[1] + array[2];
            var status = Bll_ProductIn.GetLocationSNStatus(realLocationSN);

            if (status == 2)
            {
                MessageBox.Show(txt_LocationSN.Text + "库位不存在，请检查之后重新输入库位！");
                txt_LocationSN.Text = "";
                txt_LocationSN.Focus();
                return;
            }
            txt_BoxCode.Focus();
        }
        //END

    }
}